//
//  YCMainTabVC.swift
//  YCWeiBoSwift
//
//  Created by 刘胤辰 on 16/7/19.
//  Copyright © 2016年 it.com. All rights reserved.
//

import UIKit

class YCMainTabVC: UITabBarController {

    //MARK : 懒加载控件
    lazy var composeBtn:UIButton = {
        
        let btn=UIButton()
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        btn.sizeToFit()
        
        return btn
        
    }()
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(true)
        
        tabBar.addSubview(composeBtn)
        
        tabBar.bringSubviewToFront(composeBtn)
        
        let composeBtnW=tabBar.bounds.width / CGFloat(childViewControllers.count)
        
        composeBtn.frame=CGRectInset(tabBar.bounds, composeBtnW*2, 0)
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor=UIColor.whiteColor()
        
        //MARK : 添加子控制器
        addChildViewController(YCHomeVC(), imgName: "tabbar_home", title: "首页")
        addChildViewController(YCMessageVC(), imgName: "tabbar_message_center", title: "信息")
        //中间btn
        addChildViewController(UIViewController())
        
        addChildViewController(YCDiscoverVC(), imgName: "tabbar_discover", title: "发现")
        addChildViewController(YCProfileVC(), imgName: "tabbar_profile", title: "我")
    }
    
    func addChildViewController(vc:UIViewController,imgName:String,title:String){
        
        vc.title=title
        
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
        
    vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(10)], forState: UIControlState.Normal)
        
        //设置字体的颜色
        vc.tabBarItem.image=UIImage(named: imgName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        vc.tabBarItem.selectedImage=UIImage(named:"\(imgName)_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        addChildViewController(UINavigationController(rootViewController: vc))
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
